package com.cjb.cloudsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjb.cloudsystem.entity.SysLogLogin;
import com.cjb.cloudsystem.mapper.SysLogLoginMapper;
import com.cjb.cloudsystem.service.SysLogLoginService;
import com.cjb.cloudsystem.vo.SysLogLoginVO;
import com.cjb.common.mybatisUtil.impl.BaseRepositoryImpl;
import com.cjb.common.util.AddressUtils;
import com.cjb.common.util.Common;
import com.cjb.common.util.HttpContextUtils;
import com.cjb.common.util.IpUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @Author chenjinbin
* @Package IntelliJ IDEA
* @Class SysLogLoginsServiceImpl
* @Date 2023-07-27 21:41
*/
@Service
@AllArgsConstructor
public class SysLogLoginRepositoryImpl extends BaseRepositoryImpl<SysLogLoginMapper, SysLogLogin> implements SysLogLoginService {
/**
* 日志
*/
    private final static Logger log = LoggerFactory.getLogger(SysLogLoginRepositoryImpl.class);
    /*
    * 注入mapper层
    */
    private final SysLogLoginMapper sysLogLoginMapper;
    /**
     * 单体保存
     *
     * @return
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public boolean save(SysLogLogin sysLogLogin) {
        log.info("进入保存操作");
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        //断言检测异常
        assert request != null;

        if (Common.isEmpty(sysLogLogin)){
            //捕获请求信息
            String userAgent = request.getHeader(HttpHeaders.USER_AGENT);
            String ip = IpUtils.getHostIp();
            String address = AddressUtils.getAddressByIP(ip);

            sysLogLogin.setUserAgent(userAgent);
            sysLogLogin.setIp(ip);
            sysLogLogin.setAddress(address);
        }

        log.info("成功记录登录日志信息!");
        return baseMapper.insert(sysLogLogin) > 0;
    }

    /**
    *批量保存
    *
    * @Author chenjinbin
    * @Value
    * @Title
    */
    @Override
    public Integer listSave(List<SysLogLogin> sysLogLogin) {
        return sysLogLoginMapper.insertBatchSomeColumn(sysLogLogin);
    }

    /**
    * 批量更新
    *
    * @Author chenjinbin
    * @Value
    * @Title
    */
    @Override
    public void listUpdate(List<SysLogLogin> sysLogLogin) {
        sysLogLoginMapper.insertBatchSomeColumn(sysLogLogin);
    }

    /**
    * 批量删除
    *
    * @Author chenjinbin
    * @Value
    * @Title
    */
    @Override
    public void listDelete(List<SysLogLogin> sysLogLogin) {
        List<Long> collect = sysLogLogin.stream().map(SysLogLogin::getId)
        .distinct()
        .collect(Collectors.toList());
        sysLogLoginMapper.deleteBatchIds(collect);
    }
    /**
    * 通过id删除
    *
    * @Author chenjinbin
    * @Value
    * @Title
    */
    @Override
    public int deleteById(SysLogLogin sysLogLogin) {
    return sysLogLoginMapper.deleteById(sysLogLogin.getId());
    }

    /**
    *单个查询
    *
    * @Author chenjinbin
    * @Value
    * @Title
    */
    @Override
    public SysLogLogin queryById(SysLogLogin sysLogLogin) {
        return  sysLogLoginMapper.selectById(sysLogLogin.getId());
    }

    @Override
    public Page queryPage(Page sysLogLoginPage , SysLogLoginVO sysLogLogin) {
        return sysLogLoginMapper.selectMapsPage(sysLogLoginPage, getWrapper(sysLogLogin));
    }
    /**
     *查询属性
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    private LambdaQueryWrapper<SysLogLogin> getWrapper(SysLogLoginVO query) {
        LambdaQueryWrapper<SysLogLogin> wrapper = Wrappers.lambdaQuery();
        if(Common.isNotNull(query.getId())){wrapper.eq(SysLogLogin::getId,query.getId());}
        if(Common.isNotNull(query.getUsername())){wrapper.eq(SysLogLogin::getUsername,query.getUsername());}
        if(Common.isNotNull(query.getIp())){wrapper.eq(SysLogLogin::getIp,query.getIp());}
        if(Common.isNotNull(query.getAddress())){wrapper.eq(SysLogLogin::getAddress,query.getAddress());}
        if(Common.isNotNull(query.getUserAgent())){wrapper.eq(SysLogLogin::getUserAgent,query.getUserAgent());}
        if(Common.isNotNull(query.getStatus())){wrapper.eq(SysLogLogin::getStatus,query.getStatus());}
        if(Common.isNotNull(query.getOperation())){wrapper.eq(SysLogLogin::getOperation,query.getOperation());}
        if(Common.isNotNull(query.getTenantId())){wrapper.eq(SysLogLogin::getTenantId,query.getTenantId());}
        if(Common.isNotNull(query.getCreateTime())){wrapper.eq(SysLogLogin::getCreateTime,query.getCreateTime());}
        wrapper.orderByDesc(SysLogLogin::getCreateTime);
        return wrapper;
    }

    /**
    * 返回全部信息
    *
    * @Author chenjinbin
    * @Value
    * @Title
    */
    @Override
    public List<SysLogLogin> returnAllData() {
        return sysLogLoginMapper.selectList(null);
    }

}